package testCase;

import com.google.common.collect.Range;
import com.imagotag.test.Conditions;
import com.imagotag.test.LabelIdProvider;
import com.imagotag.test.TestBuilder;
import com.imagotag.test.TestCaseResult;
import com.imagotag.test.enums.LabelConfig;
import com.imagotag.test.enums.LabelCounter;
import com.imagotag.test.enums.LabelSensor;
import com.imagotag.test.image.ImageGenerator;
import com.imagotag.test.image.Pattern;
import extensions.TestCaseWithEV;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: bigScaleLoadTest
 * @className: Fun_basicTest
 * @description:
 * @author: JLONG
 * @date: 2024/11/13 16:16
 * @version: 1.0
 */
public class Fun_basicTest {

    // 连续查询10次温度传感器数据，然后查询1次电池采样数据
    public static void SensorTest(TestBuilder testBuilder, LabelIdProvider labelIdProvider){
        testBuilder
                .registerTest("Wait for all labels to be online.", test -> test.waitForLabels(labelIdProvider, Conditions.labelsOnline()))
                .registerTest("Query temperature sensor 1/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 2/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 3/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 4/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 5/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 6/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 7/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 8/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 9/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query temperature sensor 10/10", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.TEMPERATURE_SENSOR),
                        Map.of(
                                LabelSensor.TEMPERATURE_SENSOR, Range.closed(TestConfig.minTemp, TestConfig.maxTemp)
                                )
                ))
                .registerTest("Query battery resistance", new TestCaseWithEV<TestCaseResult>(
                        test -> test.querySensors(labelIdProvider,
                                LabelSensor.BATTERY_RESISTANCE_SENSOR),
                        Map.of(
                                LabelSensor.BATTERY_RESISTANCE_SENSOR, Range.closed(0.00, TestConfig.resistance)
                                )
                ))
                .registerTest("Query battery measurement sample", test -> test.getConfigs(labelIdProvider,
                        LabelConfig.BATTERY_MEASUREMENT_SAMPLE))
                .waitBetweenTests("It's all done", 0, TimeUnit.SECONDS);

        // 执行测试脚本
        try {
            testBuilder.run(TestConfig.serviceUrl);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void imageLoad(TestBuilder testBuilder, LabelIdProvider labelIdProvider){
        testBuilder
                .registerTest("Wait for all labels to be online.", test -> test.waitForLabels(labelIdProvider, Conditions.labelsOnline()))
//                .waitBetweenTests("=========================<执行--计数器清零--任务>=========================", 0, TimeUnit.SECONDS)
//                .registerTest("Reset Counters", test -> test.resetCounters(labelIdProvider,
//                        LabelCounter.TOTAL_DISPLAY_UPDATES,
//                        LabelCounter.FAILED_DISPLAY_UPDATES,
//                        LabelCounter.BROWNOUT_RESETS))
                        ;
        for (int i = 1; i <= 24; i++) {
            sendImage(testBuilder, labelIdProvider, i);
        }
    }

    public static void sendImage(TestBuilder testBuilder, LabelIdProvider labelIdProvider, int times){
        testBuilder.waitBetweenTests("=========================<" + "第" + times + "次 图片刷新 任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("send random_black_white_red image", test -> test.sendImage(labelIdProvider, ImageGenerator.defaultImage(), 1))
                .waitBetweenTests("Wait until the display update has finished", 30, TimeUnit.SECONDS);
    }
}
